package 优先算法.递归_搜索与回溯.初学递归.两两交换链表中的节点;

/**
 *https://leetcode.cn/problems/swap-nodes-in-pairs/submissions/560878361/
 */
public  class Main {
    public static void main(String[] args) {
        Solution so = new Solution();
//        Solution.ListNode list = null;
        Solution.ListNode list = new Solution.ListNode(1);
        list.next = new Solution.ListNode(2);
        list.next.next = new Solution.ListNode(3);
        list.next.next.next = new Solution.ListNode(4);
        list.next.next.next.next = new Solution.ListNode(5);
        Solution.ListNode listNode = so.swapPairs(list);
        Solution.ListNode cur = listNode;
        while (cur != null) {
            System.out.print(cur.val + "");
            cur = cur.next;

        }
    }
}
    class Solution {
        public static class ListNode {
            int val;
            ListNode next;

            ListNode() {
            }

            ListNode(int val) {
                this.val = val;
            }

            ListNode(int val, ListNode next) {
                this.val = val;
                this.next = next;
            }
        }

        public ListNode swapPairs(ListNode head) {
            if(head == null || head.next == null ){
                return head;
            }
            ListNode tmp = swapPairs(head.next.next);;
            ListNode newHead = head.next;
            head.next = tmp;
            newHead.next = head;
            return newHead;
        }

    }

